package com.mutouren.web.org;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mutouren.common.entity.ResultCode;
import com.mutouren.common.entity.ResultInfo;
import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.modules.org.model.App;
import com.mutouren.modules.org.model.Function;
import com.mutouren.modules.org.model.Person;
import com.mutouren.modules.org.service.AppService;
import com.mutouren.modules.org.service.FunctionService;
import com.mutouren.modules.org.service.PersonService;
import com.mutouren.web.BaseApi;

@Controller
@RequestMapping(value="/api/org")
public class FunctionApi extends BaseApi {

	@Resource
	AppService appService;
	
	@Resource
	PersonService personService;	
	
	@Resource
	private FunctionService functionService;	

	@RequestMapping(value="/getFunctionsOfApp.action")
	@ResponseBody	
	public ResultInfo<List<Function>> getFunctionsOfApp(String appAlias) {
		ResultInfo<List<Function>> result = new ResultInfo<List<Function>>();
		
		// 校验
		if (StringUtils.isBlank(appAlias)) {
			result.setCode(ResultCode.PARAMERROR.value);
			result.setMessage("参数为空");
			return result;
		}
		
		// 处理
		App app = appService.getByAppAlias(appAlias);
		if ((app == null) || (app.getValidState() != ValidState.Normal)) {
			result.setCode(ResultCode.FAIL.value);
			result.setMessage("应用不存在或无效");
			return result;			
		}
		
		result.setCode(ResultCode.SUCCESS.value);
		result.setInfo(functionService.selectByAppId(app.getAppId(), false));		
		return result;
	}

	@RequestMapping(value="/getFunctionIdsOfUser.action")
	@ResponseBody	
	public ResultInfo<int[]> getFunctionIdsOfUser(String userId, String appAlias) {
		ResultInfo<int[]> result = new ResultInfo<int[]>();
		
		// 校验
		if (StringUtils.isBlank(appAlias) || StringUtils.isBlank(userId)) {
			result.setCode(ResultCode.PARAMERROR.value);
			result.setMessage("参数为空");
			return result;
		}
		
		int aUserId = NumberUtils.toInt(userId, 0);
		if (aUserId == 0) {
			result.setCode(ResultCode.PARAMERROR.value);
			result.setMessage("userId参数有误");
			return result;			
		}
				
		// 处理
		
		App app = appService.getByAppAlias(appAlias);
		if ((app == null) || (app.getValidState() != ValidState.Normal)) {
			result.setCode(ResultCode.FAIL.value);
			result.setMessage("应用不存在或无效");
			return result;			
		}
		Person person = personService.get(aUserId);
		if ((person == null) || (person.getValidState() != ValidState.Normal)) {
			result.setCode(ResultCode.FAIL.value);
			result.setMessage("人员不存在或无效");
			return result;			
		}
		
		List<Function> listFunction = personService.selectFunction(aUserId, app.getAppId());
		int[] ids = new int[listFunction.size()];		
		for(int i = 0; i < listFunction.size(); i++) {
			ids[i] = listFunction.get(i).getFuncId();
		}
		
		result.setCode(ResultCode.SUCCESS.value);
		result.setInfo(ids);		
		return result;
	}

}
